#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>

#define MAXSIZE 10

struct TreeNode {
    char element;
    int left;
    int right;
} T1[MAXSIZE], T2[MAXSIZE];

bool Isomorphic ( int r1, int r2 ) {
    if ( ( r1 == -1 ) && ( r2 == -1 ) ) return true;
    else if ( ( ( r1 != -1 ) && ( r2 == -1 ) ) || ( ( r1 == -1 ) && ( r2 != -1 ) ) ) return false;
    else if ( T1[r1].element != T2[r2].element ) return false;
    else if ( ( T1[r1].left == -1 ) && ( T2[r2].left == -1 ) ) return Isomorphic(T1[r1].right, T2[r2].right);
    else if ( ( T1[r1].left != -1 ) && ( T2[r2].left != -1 ) && ( T1[T1[r1].left].element == T2[T2[r2].left].element ) ) return ( Isomorphic(T1[r1].left, T2[r2].left ) && ( Isomorphic(T1[r1].right, T2[r2].right ) ) );
    else return ( Isomorphic(T1[r1].left, T2[r2].right ) && ( Isomorphic(T1[r1].right, T2[r2].left ) ) );
}

int main () {
    int N1, i, N2, root1, root2;
    char letter, left, right;
    /* build tree */
    scanf("%d", &N1);
    int* check = (int*)malloc(sizeof(int)*N1);
    for ( i = 0; i < N1; i++ ) check[i] = 0;
    for ( i = 0; i < N1; i++ ) {
        while ( getchar() != '\n' );
        scanf("%c %c %c", &letter, &left, &right);
        T1[i].element = letter;
        if ( isdigit(left) ) {
            T1[i].left = left - '0';
            check[T1[i].left] = 1;
        } else T1[i].left = -1;
        if ( isdigit(right) ) {
            T1[i].right = right - '0';
            check[T1[i].right] = 1;
        } else T1[i].right = -1;
    }
    for ( i = 0; i < N1; i++ ) {
        if ( check[i] == 0 ) {
            root1 = i; break;
        }
    }
    scanf("%d", &N2);
    if ( N1 != N2 ) {
        printf("No\n"); free(check); exit(0);
    } else if ( ( N1 == 0 ) && ( N2 == 0 ) ) {
        printf("Yes\n"); exit(0);
    }
    
    for ( i = 0; i < N1; i++ ) check[i] = 0;
    for ( i = 0; i < N2; i++ ) {
        while ( getchar() != '\n' );
        scanf("%c %c %c", &letter, &left, &right);
        T2[i].element = letter;
        if ( isdigit(left) ) {
            T2[i].left = left - '0';
            check[T2[i].left] = 1;
        } else T2[i].left = -1;
        if ( isdigit(right) ) {
            T2[i].right = right - '0';
            check[T2[i].right] = 1;
        } else T2[i].right = -1;
    }
    for ( i = 0; i < N2; i++ ) {
        if ( check[i] == 0 ) {
            root2 = i; break;
        }
    }
    /* check tree */
    if ( Isomorphic(root1, root2) == true ) printf("Yes\n");
    else printf("No\n");
    return 0;
}